package io.github.wppli.trigger.http;

import cc.jq1024.middleware.token.service.ITokenService;
import com.alibaba.fastjson.JSON;
import io.github.wppli.domain.authenticate.model.entity.SingInEntity;
import io.github.wppli.domain.authenticate.model.entity.SingInUserInfoEntity;
import io.github.wppli.domain.authenticate.model.entity.UserEntity;
import io.github.wppli.domain.authenticate.service.engine.factory.AuthenticateFactory;
import io.github.wppli.domain.user.service.IUserService;
import io.github.wppli.trigger.api.dto.user.FollowRequest;
import io.github.wppli.trigger.api.dto.user_authenticate.SingInResponse;
import io.github.wppli.types.enums.ResponseCode;
import io.github.wppli.types.exception.AppException;
import io.github.wppli.types.model.Response;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author li--jiaqiang 2025−02−28
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/user/")
@CrossOrigin(value = {"${app.config.cross-origin:http://116.198.239.101:3000/}", "http://localhost:3000/"})
public class UserController {

    private final IUserService userService;
    private final ITokenService tokenService;

    /**
     * 关注
     */
    @PostMapping("/follow")
    public Response<String> follow(@RequestBody FollowRequest followRequest, @RequestHeader("Authorization") String token)  {
        try {
            log.info("UserController.follow(). 关注开始执行...");
            // 1. 参数校验
            if(null == followRequest || null == followRequest.getUserId()) {
                log.warn("UserController.follow(). 参数错误: {}", JSON.toJSONString(followRequest));
                return Response.<String>builder()
                        .code(ResponseCode.ILLEGAL_PARAMETER.getCode())
                        .info(ResponseCode.ILLEGAL_PARAMETER.getInfo())
                        .build();
            }
            // 2. 业务
            String jwtToken = token.replace("Bearer ", "");
            Long userId = Long.parseLong(tokenService.decode(jwtToken).getSubject());
            userService.follow(userId, followRequest.getUserId());
            log.info("UserController.follow(). 关注执行完毕。");
            // 3. 返回结果
            return Response.ok("关注成功");
        } catch (AppException exception) {
            log.error("AppException: ",exception);
            return Response.<String>builder()
                    .code(exception.getCode())
                    .info(exception.getInfo())
                    .build();
        } catch (Exception exception) {
            log.error("Exception: ",exception);
            return Response.systemError();
        }
    }

    public static void main(String[] args) {
        System.out.println((byte)128);
    }

    /**
     * 取关
     */
    @PostMapping("/unfollow")
    public Response<String> unfollow(@RequestBody FollowRequest followRequest, @RequestHeader("Authorization") String token) {
        try {
            log.info("UserController.unfollow(). 取关开始执行...");
            // 1. 参数校验
            if(null == followRequest || null == followRequest.getUserId()) {
                log.warn("UserController.unfollow(). 参数错误: {}", JSON.toJSONString(followRequest));
                return Response.<String>builder()
                        .code(ResponseCode.ILLEGAL_PARAMETER.getCode())
                        .info(ResponseCode.ILLEGAL_PARAMETER.getInfo())
                        .build();
            }
            // 2. 业务
            String jwtToken = token.replace("Bearer ", "");
            Long userId = Long.parseLong(tokenService.decode(jwtToken).getSubject());
            userService.unfollow(userId, followRequest.getUserId());
            log.info("UserController.unfollow(). 取关执行完毕。");
            // 3. 返回结果
            return Response.ok("取关成功");
        } catch (AppException exception) {
            log.error("AppException: ",exception);
            return Response.<String>builder()
                    .code(exception.getCode())
                    .info(exception.getInfo())
                    .build();
        } catch (Exception exception) {
            log.error("Exception: ",exception);
            return Response.systemError();
        }
    }
}